package com.zadtek.shopmall.activities;

import android.content.Intent;
import android.support.annotation.Nullable;
import android.support.v4.util.ArrayMap;
import android.view.View;
import android.widget.ImageView;

import com.bumptech.glide.Glide;
import com.chad.library.adapter.base.BaseQuickAdapter;
import com.chad.library.adapter.base.BaseViewHolder;
import com.zadtek.shopmall.R;
import com.zadtek.shopmall.base.AbsListActivity;
import com.zadtek.shopmall.bean.EvaluateProductBean;
import com.zadtek.shopmall.net.BaseObserver;
import com.zadtek.shopmall.net.BaseResponse;
import com.zadtek.shopmall.net.NetTool;
import com.zadtek.shopmall.other.Const;
import com.zadtek.shopmall.utils.ToastUtil;

import java.util.LinkedList;
import java.util.List;
import java.util.Map;

import io.reactivex.android.schedulers.AndroidSchedulers;
import io.reactivex.schedulers.Schedulers;

public class EvaluateActivity extends AbsListActivity {

    private EvaluateAdapter evaluteAdapter;
    private List<EvaluateProductBean> data = new LinkedList<>();

    private int page =1 ;
    private int pagesize = 10;

    @Override
    protected String getTopTitle() {
        return "评价晒单";
    }

    @Override
    public BaseQuickAdapter getAdapter() {
        evaluteAdapter = new EvaluateAdapter(R.layout.evaluate_item,data);
        evaluteAdapter.setOnItemChildClickListener(new BaseQuickAdapter.OnItemChildClickListener() {
            @Override
            public void onItemChildClick(BaseQuickAdapter adapter, View view, int position) {
                if(view.getId() == R.id.tv_btn_evaluate){
                    ToastUtil.showToast("评价晒单");
                    startActivity(new Intent(EvaluateActivity.this,EvaluateOrderActivity.class).putExtra("data",data.get(position)));
                }
            }
        });
        return evaluteAdapter;
    }

    @Override
    public void loadMore() {
        requestData();
    }

    @Override
    protected void requestData() {
        //Map<String, Object> param = NetTool.newParams();
        Map<String, Object> param = new ArrayMap<String, Object>();
        param.put("uid", 44);
        param.put("token", Const.token);
        param.put("page",page);
        param.put("pagesize",pagesize);
        NetTool.getApi().getEvaluateList(param)
                .subscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe(new BaseObserver<BaseResponse<List<EvaluateProductBean>>>(this) {
                    @Override
                    public void onData(BaseResponse<List<EvaluateProductBean>> value) {
                        if(value.getCode() == 0 && value.getData()!=null){
                            if(value.getData().size()!=0){
                                data.addAll(value.getData());
                                evaluteAdapter.notifyDataSetChanged();
                                evaluteAdapter.loadMoreComplete();
                                page++;
                            }else{
                                evaluteAdapter.loadMoreEnd();
                            }
                        }
                    }
                });
    }

    @Override
    public void OnItemClick(int position) {
        ToastUtil.showToast("点击了+"+position);
    }

    private class EvaluateAdapter extends BaseQuickAdapter<EvaluateProductBean,BaseViewHolder>{

        public EvaluateAdapter(int layoutResId, @Nullable List<EvaluateProductBean> data) {
            super(layoutResId, data);
        }

        @Override
        protected void convert(BaseViewHolder helper, EvaluateProductBean item) {
            helper.addOnClickListener(R.id.tv_btn_evaluate);
            Glide.with(mActivity).load(item.getGoods_img()).asBitmap().centerCrop().placeholder(R.drawable.placeholder).into((ImageView) helper.getView(R.id.iv_product_img));
            helper.setText(R.id.tv_product_desc,item.getGoods_name());
        }
    }
}
